//
//  SNMYNavigationController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/11.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNMYNavigationController.h"

@interface SNMYNavigationController ()<UINavigationControllerDelegate>
@property(nonatomic, strong) id popDelegate;
@end

@implementation SNMYNavigationController
+ (void)initialize {
    
    // 设置导航条的内容
    // 获取当前应用下所有的导航条     [UINavigationBar appearance]
    
    // 获取哪个类下面的导航条
    UINavigationBar * bar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[ self ]];
    
    //	[bar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
    
    // 通过setTintColor设置导航条文字的颜色
    [bar setTintColor:HexRGB(0x818181)];
    
    // 设置导航条标题颜色
//    NSMutableDictionary * titleAttribute = [NSMutableDictionary dictionary];
//    titleAttribute[NSForegroundColorAttributeName] = [UIColor colorWithWhite:0.5 alpha:1.0];
//    titleAttribute[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
//    [bar setTitleTextAttributes:titleAttribute];
    
    // 可以跳转返回按钮文字的偏移量
    //    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -100) forBarMetrics:UIBarMetricsDefault];
}

- (void)receivedNeedAnimationNotification:(NSNotification *)notify
{
    self.needAnimation = notify.object;
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)addObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNeedAnimationNotification:   ) name:@"NeedAnimationNotification" object:nil];
   
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObserver];
    self.needAnimation = @(YES);
    // Do any additional setup after loading the view.
    //    interactivePopGestureRecognizer!.enabled = false;
//    self.view.backgroundColor = HexRGB(0xf5f5f5);
//    [self.navigationBar setShadowImage:[SNUtils imageWithColor:[UIColor clearColor]]];
//    [self.navigationBar setBackgroundImage:[SNUtils imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
    
    self.popDelegate = self.interactivePopGestureRecognizer.delegate;
    
    self.delegate = self;
    
}
// 完全展示完调用
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 如果展示的控制器是根控制器，就还原pop手势代理
    if (viewController == [self.viewControllers firstObject]) {
        
        self.interactivePopGestureRecognizer.delegate = self.popDelegate;
    }
}
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.viewControllers.count != 0) { // 非跟控制器hi
        viewController.hidesBottomBarWhenPushed = YES;
        
        // 设置导航条左边按钮的内容,把系统的返回按钮给覆盖,导航控制器的滑动返回功能就木有啦
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
        
        // 就有滑动返回功能
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    
    [super pushViewController:viewController animated:animated];
}
-(void)setNeedAnimation:(NSNumber *)needAnimation{
    _needAnimation = needAnimation;
    
    
}
/** pop方法 */
- (void)back {
    NSLog(@"self = %p",self);
    [self popViewControllerAnimated:[self.needAnimation boolValue]];
}

@end
